<?php
session_start();
if (!isset($_SESSION["admin_id"])) {
    header("Location: admin.php");
    exit;
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $ders_adi = $_POST["ders_adi"];
    $kodu = $_POST["kodu"];
    $ogretmen = $_POST["ogretmen"];
    $gun = $_POST["gun"];
    $saat = $_POST["saat"];

    try {
    $db = new PDO("mysql:host=localhost;dbname=yenicesi_2305610353;charset=utf8", "yenicesi_2305610353", "2305610353C.");
    } catch (PDOException $e) {
        die("Veritabanı bağlantı hatası: " . $e->getMessage());
    }

    $sorgu = $db->prepare("INSERT INTO dersler (ders_adi, kodu, ogretmen, gun, saat) VALUES (?, ?, ?, ?, ?)");
    $ekle = $sorgu->execute([$ders_adi, $kodu, $ogretmen, $gun, $saat]);

    if ($ekle) {
        header("Location: admin_dashboard.php?ders_ekleme=basarili");
        exit;
    } else {
        echo "Ders ekleme işlemi başarısız.";
    }
}
?>

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title>Ders Ekle</title>
    <link rel="stylesheet" href="css/admin_dashboard.css">
</head>
<body>

<header>🎓 Ders Ekle</header>

<div class="container">
    <form action="ders_ekle.php" method="POST">
        <label>Ders Adı:</label>
        <input type="text" name="ders_adi" required><br><br>

        <label>Ders Kodu:</label>
        <input type="text" name="kodu" required><br><br>

        <label>Öğretmen:</label>
        <input type="text" name="ogretmen"><br><br>

        <label>Gün:</label>
        <select name="gun">
            <option value="Pazartesi">Pazartesi</option>
            <option value="Salı">Salı</option>
            <option value="Çarşamba">Çarşamba</option>
            <option value="Perşembe">Perşembe</option>
            <option value="Cuma">Cuma</option>
        </select><br><br>

        <label>Saat:</label>
        <input type="time" name="saat" required><br><br>

        <button type="submit">Ders Ekle</button>
        <a href="admin_dashboard.php" class="back-link">⬅ Geri Dön</a>
    </form>
</div>

</body>
</html>
